<!DOCTYPE HTML>
<html lang="zh-hans" >
    <!-- Start book Python基础 -->
<!-- Start book Python基础 -->
    <head>
        <!-- head:start -->
<!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>内置属性 | Python基础</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        <meta name="author" content="姜伟">
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="gitbook/style.css">
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-prism/prism.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="./chapter07.html" />
    
    
    <link rel="prev" href="./chapter05.html" />
    

        <!-- head:end -->
<!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
<!-- body:start -->
        
    <div class="book"
        data-level="6"
        data-chapter-title="内置属性"
        data-filepath="chapter06.md"
        data-basepath="."
        data-revision="Wed Feb 19 2020 00:43:01 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="./index.html">
                
                        <i class="fa fa-check"></i>
                        
                        学习目标
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="chapter01.html">
            
                
                    <a href="./chapter01.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        面向对象介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="chapter02.html">
            
                
                    <a href="./chapter02.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        类和对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3" data-path="chapter03.html">
            
                
                    <a href="./chapter03.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        面向对象基本语法
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4" data-path="chapter04.html">
            
                
                    <a href="./chapter04.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        魔法方法1
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5" data-path="chapter05.html">
            
                
                    <a href="./chapter05.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        魔法方法2
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6" data-path="chapter06.html">
            
                
                    <a href="./chapter06.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        内置属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7" data-path="chapter07.html">
            
                
                    <a href="./chapter07.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        类属性和对象属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8" data-path="chapter08.html">
            
                
                    <a href="./chapter08.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        私有属性和方法
                    </a>
            
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本书使用 GitBook 发布
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="./" >Python基础</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h2 id="&#x5185;&#x7F6E;&#x5C5E;&#x6027;">&#x5185;&#x7F6E;&#x5C5E;&#x6027;</h2>
<p>&#x4F7F;&#x7528;&#x5185;&#x7F6E;&#x51FD;&#x6570;<code>dir</code>&#x53EF;&#x4EE5;&#x67E5;&#x770B;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x652F;&#x6301;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#xFF0C;Python&#x4E2D;&#x5B58;&#x5728;&#x7740;&#x5F88;&#x591A;&#x7684;&#x5185;&#x7F6E;&#x5C5E;&#x6027;&#x3002;</p>
<h3 id="slots">__slots__</h3>
<p>Python&#x4E2D;&#x652F;&#x6301;&#x52A8;&#x6001;&#x5C5E;&#x6027;&#xFF0C;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x901A;&#x8FC7;&#x70B9;&#x8BED;&#x6CD5;&#x76F4;&#x63A5;&#x7ED9;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;&#xFF0C;&#x4EE3;&#x7801;&#x66F4;&#x52A0;&#x7684;&#x7075;&#x6D3B;&#x3002;&#x4F46;&#x662F;&#x5728;&#x67D0;&#x4E9B;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x80FD;&#x9700;&#x8981;&#x5BF9;&#x5C5E;&#x6027;&#x8FDB;&#x884C;&#x63A7;&#x5236;&#xFF0C;&#x6B64;&#x65F6;&#xFF0C;&#x5C31;&#x524B;&#x4F7F;&#x7528;__slots__&#x5B9E;&#x73B0;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    __slots__ = (<span class="hljs-string">&apos;name&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>)
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, age)</span>:</span>
        self.name = name
        self.age = age
p = Person(<span class="hljs-string">&apos;&#x5F20;&#x4E09;&apos;</span>, <span class="hljs-number">18</span>)
p.name = <span class="hljs-string">&apos;&#x674E;&#x56DB;&apos;</span>

<span class="hljs-comment"># &#x5BF9;&#x8C61;p&#x53EA;&#x80FD;&#x8BBE;&#x7F6E;name&#x548C;age&#x5C5E;&#x6027;&#xFF0C;&#x4E0D;&#x80FD;&#x518D;&#x52A8;&#x6001;&#x6DFB;&#x52A0;&#x5C5E;&#x6027;</span>
<span class="hljs-comment"># p.height = 180 # &#x62A5;&#x9519;</span>
</code></pre>
<h3 id="doc">__doc__</h3>
<p>&#x8868;&#x793A;&#x7C7B;&#x7684;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span>:</span>
    <span class="hljs-string">&quot;&quot;&quot; &#x63CF;&#x8FF0;&#x7C7B;&#x4FE1;&#x606F;&#xFF0C;&#x8FD9;&#x662F;&#x7528;&#x4E8E;&#x770B;&#x7247;&#x7684;&#x795E;&#x5947; &quot;&quot;&quot;</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-keyword">pass</span>

print(Foo.__doc__)
<span class="hljs-comment">#&#x8F93;&#x51FA;&#xFF1A;&#x7C7B;&#x7684;&#x63CF;&#x8FF0;&#x4FE1;&#x606F;</span>
</code></pre>
<h3 id="module-&#x548C;-class">__module__ &#x548C; __class__</h3>
<p>__module__ &#x8868;&#x793A;&#x5F53;&#x524D;&#x64CD;&#x4F5C;&#x7684;&#x5BF9;&#x8C61;&#x5728;&#x90A3;&#x4E2A;&#x6A21;&#x5757;&#xFF1B;__class__ &#x8868;&#x793A;&#x5F53;&#x524D;&#x64CD;&#x4F5C;&#x7684;&#x5BF9;&#x8C61;&#x7684;&#x7C7B;&#x662F;&#x4EC0;&#x4E48;&#x3002;</p>
<pre><code class="lang-python">test.py
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span><span class="hljs-params">(object)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self)</span>:</span>
        self.name = <span class="hljs-string">&apos;laowang&apos;</span>
main.py
<span class="hljs-keyword">from</span> test <span class="hljs-keyword">import</span> Person

obj = Person()
print(obj.__module__)  <span class="hljs-comment"># &#x8F93;&#x51FA; test &#x5373;&#xFF1A;&#x8F93;&#x51FA;&#x6A21;&#x5757;</span>
print(obj.__class__)  <span class="hljs-comment"># &#x8F93;&#x51FA; test.Person &#x5373;&#xFF1A;&#x8F93;&#x51FA;&#x7C7B;</span>
</code></pre>
<h3 id="dict">__dict__</h3>
<p>&#x4EE5;&#x5B57;&#x5178;&#x7684;&#x5F62;&#x5F0F;&#xFF0C;&#x663E;&#x793A;&#x5BF9;&#x8C61;&#x6240;&#x6709;&#x7684;&#x5C5E;&#x6027;&#x548C;&#x65B9;&#x6CD5;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Province</span><span class="hljs-params">(object)</span>:</span>
    country = <span class="hljs-string">&apos;China&apos;</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__init__</span><span class="hljs-params">(self, name, count)</span>:</span>
        self.name = name
        self.count = count

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">func</span><span class="hljs-params">(self, *args, **kwargs)</span>:</span>
        print(<span class="hljs-string">&apos;func&apos;</span>)

<span class="hljs-comment"># &#x83B7;&#x53D6;&#x7C7B;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x5373;&#xFF1A;&#x7C7B;&#x5C5E;&#x6027;&#x3001;&#x65B9;&#x6CD5;&#x3001;</span>
print(Province.__dict__)
<span class="hljs-comment"># &#x8F93;&#x51FA;&#xFF1A;{&apos;__dict__&apos;: &lt;attribute &apos;__dict__&apos; of &apos;Province&apos; objects&gt;, &apos;__module__&apos;: &apos;__main__&apos;, &apos;country&apos;: &apos;China&apos;, &apos;__doc__&apos;: None, &apos;__weakref__&apos;: &lt;attribute &apos;__weakref__&apos; of &apos;Province&apos; objects&gt;, &apos;func&apos;: &lt;function Province.func at 0x101897950&gt;, &apos;__init__&apos;: &lt;function Province.__init__ at 0x1018978c8&gt;}</span>

obj1 = Province(<span class="hljs-string">&apos;&#x5C71;&#x4E1C;&apos;</span>, <span class="hljs-number">10000</span>)
print(obj1.__dict__)
<span class="hljs-comment"># &#x83B7;&#x53D6; &#x5BF9;&#x8C61;obj1 &#x7684;&#x5C5E;&#x6027;</span>
<span class="hljs-comment"># &#x8F93;&#x51FA;&#xFF1A;{&apos;count&apos;: 10000, &apos;name&apos;: &apos;&#x5C71;&#x4E1C;&apos;}</span>

obj2 = Province(<span class="hljs-string">&apos;&#x5C71;&#x897F;&apos;</span>, <span class="hljs-number">20000</span>)
print(obj2.__dict__)
<span class="hljs-comment"># &#x83B7;&#x53D6; &#x5BF9;&#x8C61;obj1 &#x7684;&#x5C5E;&#x6027;</span>
<span class="hljs-comment"># &#x8F93;&#x51FA;&#xFF1A;{&apos;count&apos;: 20000, &apos;name&apos;: &apos;&#x5C71;&#x897F;&apos;}</span>
</code></pre>
<h3 id="getitem&#x3001;setitem&#x548C;delitem&#x65B9;&#x6CD5;">__getitem_<em>&#x3001;\</em>_setitem__&#x548C;__delitem__&#x65B9;&#x6CD5;</h3>
<p>&#x8FD9;&#x4E09;&#x4E2A;&#x65B9;&#x6CD5;&#xFF0C;&#x662F;&#x5C06;&#x5BF9;&#x8C61;&#x5F53;&#x505A;&#x5B57;&#x5178;&#x4E00;&#x6837;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#x3002;</p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span><span class="hljs-params">(object)</span>:</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__getitem__</span><span class="hljs-params">(self, key)</span>:</span>
        print(<span class="hljs-string">&apos;__getitem__&apos;</span>, key)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__setitem__</span><span class="hljs-params">(self, key, value)</span>:</span>
        print(<span class="hljs-string">&apos;__setitem__&apos;</span>, key, value)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">__delitem__</span><span class="hljs-params">(self, key)</span>:</span>
        print(<span class="hljs-string">&apos;__delitem__&apos;</span>, key)

obj = Foo()

result = obj[<span class="hljs-string">&apos;k1&apos;</span>]      <span class="hljs-comment"># &#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x6267;&#x884C; __getitem__</span>
obj[<span class="hljs-string">&apos;k2&apos;</span>] = <span class="hljs-string">&apos;laowang&apos;</span>   <span class="hljs-comment"># &#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x6267;&#x884C; __setitem__</span>
<span class="hljs-keyword">del</span> obj[<span class="hljs-string">&apos;k1&apos;</span>]           <span class="hljs-comment"># &#x81EA;&#x52A8;&#x89E6;&#x53D1;&#x6267;&#x884C; __delitem__</span>
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="./chapter05.html" class="navigation navigation-prev " aria-label="Previous page: 魔法方法2"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="./chapter07.html" class="navigation navigation-next " aria-label="Next page: 类属性和对象属性"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="gitbook/app.js"></script>

    
    <script src="gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-copy-code-button/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"prism":{},"splitter":{},"copy-code-button":{},"toggle-chapters":{},"highlight":{},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
<!-- body:end -->
    </body>
    <!-- End of book Python基础 -->
<!-- End of book Python基础 -->
</html>
